package com.examination.auth.handler.exception;

import com.examination.domain.common.ResponseResult;
import com.examination.enums.AppHttpCodeEnum;
import com.examination.exception.MySystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义异常
     */
    @ExceptionHandler(MySystemException.class)
    public ResponseResult systemExceptionHandler(MySystemException e){
        //打印异常信息
        log.error("出现了异常：{}",e);
        //从异常对象获取信息并封装返回
        return ResponseResult.errorResult(e.getCode(),e.getMsg());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e){
        //打印异常信息
        log.error("出现了异常：{}",e);
        //从异常对象获取信息并封装返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
    }
}
